/**
 * 
 */
package com.pdt.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pdt.dao.MerchantsDAO;
import com.pdt.dao.OutboundDAO;
import com.pdt.dao.RobotDAO;
import com.pdt.dao.UserDAO;
import com.pdt.model.Merchants;
import com.pdt.model.Outbound;
import com.pdt.model.Robot;
import com.pdt.model.User;
import com.pdt.util.CharacterUtil;
import com.pdt.util.DateUtil;
import com.pdt.util.RandomUtil;

/**
 * @author 作者：Zhangyangyang
 * @version 创建时间：2017年6月16日 下午1:42:35
 * 类说明
 */
@Service
public class RobotServiceDAOImpl implements RobotServiceDAO{
	
	@Autowired
	private MerchantsDAO merchantsDAO;
	
	@Autowired
	private RobotDAO robotDAO;
	
	@Autowired
	private OutboundDAO outboundDAO;
	
	@Autowired
	private UserDAO userDAO;

	/**
	 * 登录
	 * data包括：
	 * account:  账号
	 * password: 密码
	 */
	@Override
	public JSONObject login(JSONObject data) {
		JSONObject result = new JSONObject();
		String []str = {"account","password","user_type"};
		if (CharacterUtil.judgeJsonFormat(str, data)) {
			User user = userDAO.findUserByAccount(data.getString("account"));
			if (user != null) {
				if (data.getString("password").equals(user.getPassword()) && data.getString("user_type").equals(user.getUser_type())) {
					result.put("status", 200);
					result.put("message", "ok");
				}else {
					//密码错误
					result.put("status", 500);
					result.put("message", "Password mistake or no legal power");
				}
			}else {
				//用户不存在
				result.put("status", 500);
				result.put("message", "The user does not exit");
			}
		}else {
			//json格式或参数错误
			result.put("status", 500);
			result.put("message", "Json format error");
		}
		return result;
	}

	/**
	 * 机器人入库
	 * data包括：
	 * sn: 机器人唯一识别码
	 * ……
	 */
	@Override
	public JSONObject inHouse(JSONObject data) {
		JSONObject result = new JSONObject();
		String []str = {"sn","aiui","dlp","up_computer","up_plate","down_plate","radar",
				        "walk_driven_left","walk_driven_right","navigation","robot_type","robot_version"};
		if (CharacterUtil.judgeJsonFormat(str, data)) {
			
			Robot robot = robotDAO.findRobotBySn(data.getString("sn"));
			if (robot != null) {
				//机器人以及存在
				result.put("status", 500);
				result.put("message", "The robot already exits");
			}else {
				robot = new Robot();
				robot.setId(RandomUtil.generateString());
				robot.setAiui(data.getString("aiui"));
				robot.setDlp(data.getString("dlp"));
				robot.setSn(data.getString("sn"));
				robot.setUp_computer(data.getString("up_computer"));
				robot.setUp_plate(data.getString("up_plate"));
				robot.setDown_plate(data.getString("down_plate"));
				robot.setRadar(data.getString("radar"));
				robot.setWalk_driven_left(data.getString("walk_driven_left"));
				robot.setWalk_driven_right(data.getString("walk_driven_right"));
				robot.setNavigation(data.getString("navigation"));
				
				robot.setIn_house_time(DateUtil.getFormatTime());
				robot.setIn_house_valid(1);
				
				robot.setOperate_time(DateUtil.getFormatTime());
				robot.setOperate_user("");
				
				robot.setOut_house_time(DateUtil.getFormatTime());
				robot.setOut_house_valid(0);
				
				robot.setRobot_type(data.getString("robot_type"));
				robot.setRobot_version(data.getString("robot_version"));
				
				robot.setUpdate_time(DateUtil.getFormatTime());
				robot.setUpdate_user("");
				
				int n = robotDAO.insert(robot);
				if (n == 1) {
					result.put("status", 200);
					result.put("message", "ok");
				}else {
					//数据库操作错误
					result.put("status", 500);
					result.put("message", "Database operation error");
				}
			}
			
			
		}else {
			//json格式或数据错误
			result.put("status", 500);
			result.put("message", "Json format or data error");
		}
		return result;
	}

	/**
	 * 机器人出库
	 * data包括：
	 * sn: 机器人唯一识别码
	 * ……
	 */
	@Override
	public JSONObject outHouse(JSONObject data) {
		JSONObject result = new JSONObject();
		String []str = {"sn"};
		if (CharacterUtil.judgeJsonFormat(str, data)) {
			Outbound outbound = outboundDAO.findOutboundBySn(data.getString("sn"));
			if (outbound != null) {
				//出库记录以及存在
				result.put("status", 500);
				result.put("message", "The outbound already exits");
			}else{
				outbound = new Outbound();
				outbound.setId(RandomUtil.generateString());
				outbound.setOperate_time(DateUtil.getFormatTime());
				outbound.setOut_house_time(DateUtil.getFormatTime());
				outbound.setSn(data.getString("sn"));
				outbound.setUpdate_time(DateUtil.getFormatTime());
				outbound.setUpdate_user("");
				
				Robot robot = robotDAO.findRobotBySn(data.getString("sn"));
				robot.setOut_house_valid(1);
				robot.setOut_house_time(DateUtil.getFormatTime());
				robotDAO.updateByPrimaryKey(robot);
				
				int n = outboundDAO.insert(outbound);
				if (n == 1) {
					result.put("status", 200);
					result.put("message", "ok");
				}else {
					//数据库操作错误
					result.put("status", 500);
					result.put("message", "Database operation error");
				}
			}
		}else {
			//josn格式或数据错误
			result.put("status", 500);
			result.put("message", "Json format or data error");
		}
		return result;
	}

	/**
	 * 机器人查询sn
	 * data包括：
	 * 相关机器人码
	 * ……
	 */
	@Override
	public JSONObject searchSN(JSONObject data) {
		JSONObject result = new JSONObject();
		String []str = {"up_computer"};
		if (CharacterUtil.judgeJsonFormat(str, data)) {
			Robot robot = robotDAO.findRobotByOtherParam(data.getString("up_computer"));
			if (robot != null) {
				result.put("status", 200);
				result.put("message", "ok");
				result.put("sn", robot.getSn());
			}else {
				//机器人不存在
				result.put("status", 500);
				result.put("message", "The robot does not exit");
			}
		}else {
			//json格式或数据错误
			result.put("status", 500);
			result.put("message", "Json format or data error");
		}
		return result;
	}

	/**
	 * 添加商户信息
	 * data包括：
	 * 商户名称、管理员账号、密码等
	 * ……
	 */
	@Override
	public JSONObject addMerchants(JSONObject data) {
		JSONObject result = new JSONObject();
		String []str = {"name","introduction","SNs"};
		if (CharacterUtil.judgeJsonFormat(str, data)) {
			String mer_id = RandomUtil.generateString();
			JSONArray array = data.getJSONArray("SNs");
			Merchants merchants = new Merchants();
			for (Object object : array) {
				Outbound outbound = outboundDAO.findOutboundBySn(object.toString());
				if (outbound != null) {
					if (outbound.getMerchant_id() != null && outbound.getMerchant_id() != "") {
						//机器人已经添加
						result.put("status", 500);
						result.put("message", "The outbound already distributed");
					}else {
						String account = RandomUtil.getCreditCode();
						String password = RandomUtil.generateString(6);
						outbound.setBound_time(DateUtil.getFormatTime());
						outbound.setMerchant_id(mer_id);
						outbound.setUpdate_time(DateUtil.getFormatTime());
						outbound.setUpdate_user("");
						
						merchants.setAccount(account);
						merchants.setPassword(password);
						merchants.setCreate_time(DateUtil.getFormatTime());
						merchants.setCreater("");
						merchants.setId(mer_id);
						merchants.setIntroduction(data.getString("introduction"));
						merchants.setName(data.getString("name"));
						merchants.setUpdate_one("");
						merchants.setUpdate_time(DateUtil.getFormatTime());
						
						int n = outboundDAO.updateByPrimaryKey(outbound);
						if (n == 1) {
							merchantsDAO.insert(merchants);
							result.put("status", 200);
							result.put("message", "ok");
							result.put("account", account);
							result.put("password", password);
						}
					}
					
				}else{
					//出库记录不存在
					result.put("status", 500);
					result.put("message", "The outbound does not exit");
				}
			}
			
		}else {
			//json格式错误
			result.put("status", 500);
			result.put("message", "Json format or data error");
		}
		return result;
	}

	@Override
	public JSONObject getRobotBySn(JSONObject data) {
		JSONObject result = new JSONObject();
		String []str = {"sn"};
		if (CharacterUtil.judgeJsonFormat(str, data)) {
			Robot robot = robotDAO.findRobotBySn(data.getString("sn"));
			if (robot != null) {
				result.put("status", 200);
				result.put("message", "ok");
				result.put("sn", robot.getSn());
				result.put("up_computer",robot.getUp_computer() );
				result.put("up_plate", robot.getUp_plate());
				result.put("down_plate", robot.getDown_plate());
				result.put("radar", robot.getRadar());
				result.put("dlp", robot.getDlp());
				result.put("aiui", robot.getAiui());
				result.put("walk_driven_left", robot.getWalk_driven_left());
				result.put("walk_driven_right", robot.getWalk_driven_right());
				result.put("navigation", robot.getNavigation());
			}else {
				//sn不存在
				result.put("status", 500);
				result.put("message", "The SN does not exit");
			}
		}else {
			//json格式错误
			result.put("status", 500);
			result.put("message", "Json format or data error");
		}
		return result;
	}
}
